<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "region".
 *
 * @property int $id ID
 * @property int $code
 * @property string $name
 * @property double $pid
 * @property int $r_level
 * @property int $agent_uid 代理人UID
 * @property string $en_name
 * @property string $short_en_name
 */
class Region extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'region';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['code', 'r_level', 'agent_uid'], 'integer'],
            [['name', 'pid'], 'required'],
            [['pid'], 'number'],
            [['name', 'en_name'], 'string', 'max' => 100],
            [['short_en_name'], 'string', 'max' => 10],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => 'Code',
            'name' => 'Name',
            'pid' => 'Pid',
            'r_level' => 'R Level',
            'agent_uid' => '代理人UID',
            'en_name' => 'En Name',
            'short_en_name' => 'Short En Name',
        ];
    }
    
    public static function getFirstRegions(){
        $sql = "select id,name from region where pid=1";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryAll();
    }
    
    public static function getSubRegions($region_id){
        $sql = "select id,name,agent_uid from region where pid=$region_id";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryAll();
    }
    
    public static function getRegionNameById($id){
        $sql = "select name from region where id=$id";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryOne();
        return $data['name'];
    }
}
